package com.qf.area.service.impl;

import com.qf.area.entity.Area;
import com.qf.area.mapper.AreaMapper;
import com.qf.area.service.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dto.PageDto;
import com.qf.vo.AreaVo;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 行政区划表 服务实现类
 * </p>
 *
 * @author xjj
 * @since 2023-10-31
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Result<List<AreaVo>> findAll() {
        List<AreaVo> province = areaMapper.findAll();

        return new Result(0,"success",province);
    }

    @Override
    public PageResult<Area> pageInfo(PageDto<Area> pageDto) {
        Integer code = null;
        String name = null;
        //条件判断
        if (!StringUtils.isEmpty(pageDto.getWhere().getCode())){
            code = pageDto.getWhere().getCode();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())){
            name = pageDto.getWhere().getName();
        }
        //获取所有的Area列表
        List<Area> areaList = areaMapper.findList(pageDto, code, name);
        //查询总条数
        Long count = areaMapper.count(pageDto, code, name);
        //计算总页数
        Long pages = (count + pageDto.getSize() - 1) / pageDto.getSize();
        //封装数据
        return new PageResult<>(count,pages,areaList);
    }
}
